package com.burgeon.backend.Rest.coupon.util;
/**
 * 通过会员等级查询会员
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.VipQuery;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindVipQuery extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindVipQuery(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<String> getData(VipQuery mesage) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(mesage), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        if (!"0".equals(code)){
            throw new Exception(str);
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<String> data = new ArrayList<String>();

        for (int i = 0; i < jsonArray.length(); i++) {
            data.add(jsonArray.getJSONArray(i).getString(0));
        }
        return data;
    }

    private MultiValueMap Data(VipQuery mesage) {

        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("CARDNO");

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名
        //********************************************************
        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();
        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();

        HashMap expr1v1v1 = new HashMap();
        HashMap expr1v1v2 = new HashMap();
        HashMap expr1v2v1 = new HashMap();
        HashMap expr1v2v2 = new HashMap();
        HashMap expr2v1v1 = new HashMap();
        HashMap expr2v1v2 = new HashMap();
        HashMap expr2v2v1 = new HashMap();
        HashMap expr2v2v2 = new HashMap();

        paramss.put("combine", "and");
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        expr1.put("combine","and");
        expr1.put("expr1",expr1v1);
        expr1.put("expr2",expr1v2);
        expr2.put("combine","and");
        expr2.put("expr1",expr2v1);
        expr2.put("expr2",expr2v2);

        expr1v1.put("combine","and");
        expr1v1.put("expr1",expr1v1v1);
        expr1v1.put("expr2",expr1v1v2);
        expr1v2.put("combine","and");
        expr1v2.put("expr1",expr1v2v1);
        expr1v2.put("expr2",expr1v2v2);
        expr2v1.put("combine","and");
        expr2v1.put("expr1",expr2v1v1);
        expr2v1.put("expr2",expr2v1v2);
        expr2v2.put("combine","and");
        expr2v2.put("expr1",expr2v2v1);
        expr2v2.put("expr2",expr2v2v2);

        expr1v1v1.put("column","CARDNO");//卡号
        expr1v1v1.put("condition", "'"+mesage.getCode()+"'");
        expr1v1v2.put("column","'C_VIPTYPE_ID;NAME'");//VIP卡类型
        expr1v1v2.put("condition", "'"+mesage.getType()+"'");
        expr1v2v1.put("column","'C_CUSTOMER_ID;NAME'");//所属经销商
        expr1v2v1.put("condition", "'"+mesage.getDealer()+"'");
        expr1v2v2.put("column","'C_STORE_ID;NAME'");//所属店仓
        expr1v2v2.put("condition","'"+ mesage.getStore()+"'");
        expr2v1v1.put("column","'OPENCARDDATE'");//开卡时间
        expr2v1v1.put("condition", "'"+mesage.getCreatetime()+"'");
        expr2v1v2.put("column","'AGE'");//年龄
        expr2v1v2.put("condition", "'"+mesage.getAge()+"'");

        HashMap expr2v2v1v1 = new HashMap();
        HashMap expr2v2v1v2 = new HashMap();
        expr2v2v1v1.put("column","'BIRTHMONTH'");//生日
        expr2v2v1v1.put("condition","'"+ mesage.getBirthday()+"'");
        expr2v2v1v2.put("column","'HR_EMPLOYEE_ID;NAME'");//开卡人
        expr2v2v1v2.put("condition","'"+ mesage.getEmp()+"'");

        HashMap expr2v2v2v1 = new HashMap();
        HashMap expr2v2v2v2 = new HashMap();
        expr2v2v2v1.put("column","'C_STORE_ID;C_AREA_ID;NAME'");//姓名
        expr2v2v2v1.put("condition","'"+ mesage.getArea()+"'");
        expr2v2v2v2.put("column","'SEX'");//性别
        expr2v2v2v2.put("condition", "'"+mesage.getGender()+"'");


        expr2v2v1.put("combine","and");
        expr2v2v1.put("expr1",expr2v2v1v1);
        expr2v2v1.put("expr2",expr2v2v1v2);
        expr2v2v2.put("combine","and");
        expr2v2v2.put("expr1",expr2v2v2v1);
        expr2v2v2.put("expr2",expr2v2v2v2);


        //********************************************************
        params.put("params",paramss);
        params.put("count","true");
        params.put("range","50000");

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindVipQuery g = new FindVipQuery("39.100.45.8:55322", "nea@burgeon.com.cn", "123");
        try {
            VipQuery data = new VipQuery();
            g.getData(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
